package com.aibaixun.web.controller.system;

import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.aibaixun.common.core.domain.R;
import com.aibaixun.system.domain.EquipmentManage;
import com.aibaixun.system.domain.dto.UpdateEquipmentManageDto;
import com.aibaixun.system.domain.vo.ExportEquipmentManageVo;
import com.aibaixun.system.service.EquipmentManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * <b>EquipmentManageController</b>
 *
 * @description: EquipmentManageController <br>
 * @date: 2024/7/23 16:21 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Api(tags = "配线数据管理")
@RestController
public class EquipmentManageController {
    private static final Logger logger = LoggerFactory.getLogger(EquipmentManageController.class);

    @Autowired
    private EquipmentManageService equipmentManageService;

    @ApiOperation("批量导入配线数据")
    @PostMapping("/upload/equipmentManage")
    public R uploadEquipmentManage(@RequestParam("file") MultipartFile file){
        equipmentManageService.uploadEquipmentManage(file);
        return R.ok();
    }

    @ApiOperation("导出配线数据")
    @GetMapping("/export/equipmentManage")
    public void exportEquipmentManage(HttpServletResponse response) throws IOException {
        List<ExportEquipmentManageVo> equipmentManageList = equipmentManageService.exportEquipmentManage();
        //创建xlsx格式的
        ExcelWriter writer = ExcelUtil.getWriter();
        //自定义标题别名
        writer.addHeaderAlias("wiringNo","配线编号");
        writer.addHeaderAlias("computerRoomName","所属机房");
        writer.addHeaderAlias("equipmentNoP1","P1端设备编号");
        writer.addHeaderAlias("equipmentNoP2","P2端设备编号");
        writer.addHeaderAlias("equipmentNameNoP1","P1端设备名称");
        writer.addHeaderAlias("equipmentNameNoP2","P2端设备名称");

        //默认配置
        writer.write(equipmentManageList,true);
        //设置content—type
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");
        //设置标题
        String fileName= URLEncoder.encode("配线数据管理","UTF-8");
        //Content-disposition是MIME协议的扩展，MIME协议指示MIME用户代理如何显示附加的文件。
        response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
        ServletOutputStream outputStream= response.getOutputStream();

        //将Writer刷新到OutPut
        writer.flush(outputStream,true);
        outputStream.close();
        writer.close();
    }

    @ApiOperation("新增配线数据")
    @PostMapping("/create/equipmentManage")
    public R createEquipmentManage(@RequestBody ExportEquipmentManageVo dto){
        boolean status = equipmentManageService.createEquipmentManage(dto);
        return R.ok(status);
    }

    @ApiOperation("分页查询配线数据管理列表")
    @GetMapping("/query/equipmentManage/list")
    public R queryEquipmentManageList(@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(required = false) String wiringNo){
        IPage<EquipmentManage> list = equipmentManageService.queryEquipmentManageList(pageNum,pageSize,wiringNo);
        return R.ok(list);
    }

    @ApiOperation("根据ID修改配线数据")
    @PostMapping("/update/equipmentManage")
    public R updateEquipmentManage(@RequestBody UpdateEquipmentManageDto dto){
        boolean status = equipmentManageService.updateEquipmentManage(dto);
        return R.ok(status);
    }

    @ApiOperation("根据ID删除配线数据信息")
    @GetMapping("/delete/equipmentManage")
    public R deleteEquipmentManage(@RequestParam Long id){
        boolean status = equipmentManageService.deleteEquipmentManage(id);
        return R.ok(status);
    }
}
